package com.weaver.smartext.controller;

import com.weaver.smartext.core.domain.ResultBean;
import com.weaver.smartext.core.domain.StructablePdfDto;
import com.weaver.smartext.core.domain.StructableTextDto;
import com.weaver.smartext.core.domain.TextAnalysis;
import com.weaver.smartext.core.handler.ParserHandler;
import com.weaver.smartext.core.parser.PdfParser;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @Description: 上传测试类
 * @title: TextController
 * @Author Star_Chen
 * @Date: 2023/3/31 14:49
 * @Version 1.0
 */
@RestController
public class TextController {

    /**
     * @Author Star_Chen
     * @Description tika 读取
     * @Date 18:48 2023/4/6
     * @Param [file]
     * @return com.weaver.smarttext.core.domain.ResultBean
     **/
    @PostMapping("upload")
    public ResultBean upload(@RequestParam(name = "file", required = false)
                             MultipartFile[] file) {
        ResultBean resultBean = ResultBean.newInstance();
        StructablePdfDto structablePdfDto = new StructablePdfDto();
        TextAnalysis text = new TextAnalysis();

        for (MultipartFile multipartFile : file) {
            try {
                text.setInputStream(multipartFile.getInputStream());
                text.setSuffix(multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().lastIndexOf(".") + 1));
            } catch (IOException e) {
                e.printStackTrace();
            }
            ParserHandler parserHandler = new ParserHandler();
            structablePdfDto = parserHandler.parse(text);

        }

        return resultBean.ok(structablePdfDto);

    }
    
    /**
     * @Author Star_Chen
     * @Description pdfbox直接读取本地文件
     * @Date 21:53 2023/4/4
     * @Param []
     * @return com.weaver.smarttext.core.domain.ResultBean
     **/
    @PostMapping("uploadBox")
    public ResultBean uploadBox(){
        ResultBean resultBean = ResultBean.newInstance();

        PdfParser parser = new PdfParser();
        StructablePdfDto data = parser.parser();

        return resultBean.ok(data);
    }


    @PostMapping("text")
    public ResultBean text(@RequestParam(name = "file", required = false)
                               MultipartFile file){

        ResultBean resultBean = ResultBean.newInstance();
        TextAnalysis text = new TextAnalysis();
        try {
            text.setInputStream(file.getInputStream());
            text.setSuffix(file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1));
        } catch (IOException e) {
            e.printStackTrace();
        }
        ParserHandler parserHandler = new ParserHandler();
        StructableTextDto structableTextDto = parserHandler.parse(text);
        return resultBean.ok(structableTextDto);
    }
}
